<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Msg extends CIC_A_Controller
{

	function __construct()
	{
		parent::__construct();
	}
	
	function index()
	{
		$data	= $this->get_message();
		$this->load->view('admin/msg/index',$data);
	}
	
	function get_message()
	{
		$url = site_url('admin/msg/index?');
		$data['page']	 = $this->page($url, $this->cic_model->get_total_rows('msg'), $this->per_page,TRUE);
		$data['msg'] = $this->cic_model->get_all('msg','',array('msg_create_time','desc'),$this->per_page,$this->input->get('per_page'));
		return $data;
	}
	
	function msg_reply()
	{
		$msg = $this->cic_model->get_one('msg',array('msg_id'=>$this->uri->segment(4)));
		if($msg->msg_status == '0')
			$this->cic_model->update('msg',array('msg_status'=>1),array('msg_id'=>$this->uri->segment(4)));
		$data['msg']	= $this->cic_model->get_one('msg',array('msg_id'=>$this->uri->segment(4)),array('msg_create_time','desc'));
		$this->load->view('admin/msg/msg_reply',$data);
	}
	
	function reply_msg()
	{
		$this->validate('reply_msg', 'admin/msg/msg_reply/'.$this->uri->segment(4));
		$data = array(
				'msg_status'		=> 2,
				'msg_reply_content'	=> $this->input->post('msg_reply_content'),
				'msg_reply_time'	=> time(),
		);
		if($this->cic_model->update('msg',$data,array('msg_id'=>$this->uri->segment(4))))
		{
			$this->message->msg('回复成功！',site_url('admin/msg/index'));
		}
	}
	
	function shenhe_msg()
	{
		if($this->cic_model->update('msg',array('msg_shenhe'=>1),array('msg_id'=>$this->input->post('msg_id'))))
		{
			echo 1;
		}
	}
	
	function content_add()
	{
		$this->load->view('admin/msg/content_add');
	}
	
	function content_edit()
	{
		$data = array(
				'msg'			=> $this->cic_model->get_one('msg',array('msg_id'=>$this->uri->segment(4))),
		);
		$this->load->view('admin/msg/content_edit',$data);
	}
	function add_content()
	{
		$this->validate('msg', 'admin/msg/content_add');
		$data = array(
				'msg_name'		=> $this->input->post('msg_name'),
				'msg_content'		=> $this->input->post('msg_content',FALSE),	
		);
		if($this->cic_model->insert('msg',$data))
		{
			$this->msg->msg('添加成功！',site_url('admin/msg/index'));
		}else{
			$this->msg->msg('添加失败！',site_url('admin/msg/content_add'));
		}
	}
	
	
	
	function get_content()
	{
		$data['msg']		 = $this->cic_model->get_all('msg','','',$this->per_page,$this->uri->segment(4,0));
		$data ['full']		 = FALSE;
		echo json_encode(array('msg'=>$this->load->view('admin/msg/index',$data,TRUE),'page'=>$this->page('javascript:get_content',$this->cic_model->get_total_rows('msg'),$this->per_page,4)));
	}
	
	
	function delete_content()
	{
		if($this->cic_model->delete('msg',array('msg_id'=>$this->input->post('msg_id'))))
		{
			echo 1;
		}else{
			echo 0;
		}
	}
	
	function msg_config()
	{
		$data['site_config'] = $this->get_config('ciccms/site');
		$this->load->view('admin/msg/msg_config',$data);
	}
	
	function update_msg_config()
	{
		if($this->_write_config($this->input->post(), APPPATH.'config/ciccms/site.php'))
		{
			$this->message->msg('操作成功！',site_url('admin/msg/msg_config'));
		}else{
			$this->message->msg('操作失败！',site_url('admin/msg/msg_config'));
		}
	}
	function _write_config($data,$path)
	{
		$config = file_get_contents($path);
		foreach($data as $key => $value)
		{
			$pattern = '/\'('.$key.')\'(\s*)=>(\s*)\'(.*?)\'/is';
			$config = preg_replace($pattern, '\'\\1\'\\2=>\\3\''.$value.'\'', $config);
		}
		return file_put_contents($path,$config);
	}
}